1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *   - Redistributions of source code must retain the above copyright
9    *     notice, this list of conditions and the following disclaimer.
10   *
11   *   - Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in the
13   *     documentation and/or other materials provided with the distribution.
14   *
15   *   - Neither the name of Oracle nor the names of its
16   *     contributors may be used to endorse or promote products derived
17   *     from this software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  
33  import javax.swing.*;
34  import javax.swing.table.*;
35  
36  import java.awt.event.WindowAdapter;
37  import java.awt.event.WindowEvent;
38  import java.awt.Dimension;
39  import java.util.logging.Level;
40  import java.util.logging.Logger;
41  import javax.swing.UIManager.LookAndFeelInfo;
42  
43  
44  /**
45   * An example showing the JTable with a dataModel that is not derived
46   * from a database. We add the optional TableSorter object to give the
47   * JTable the ability to sort.
48   *
49   * @author Philip Milne
50   */
51  public class TableExample3 {
52  
53      public TableExample3() {
54          JFrame frame = new JFrame("Table");
55          frame.addWindowListener(new WindowAdapter() {
56  
57              @Override
58              public void windowClosing(WindowEvent e) {
59                  System.exit(0);
60              }
61          });
62  
63          // Take the dummy data from SwingSet.
64          final String[] names = { "First Name", "Last Name", "Favorite Color",
65              "Favorite Number", "Vegetarian" };
66          final Object[][] data = {
67              { "Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE },
68              { "Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE },
69              { "Alan", "Chung", "Green", new Integer(838), Boolean.FALSE },
70              { "Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE },
71              { "Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE },
72              { "Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE },
73              { "James", "Gosling", "Pink", new Integer(21), Boolean.FALSE },
74              { "David", "Karlton", "Red", new Integer(1), Boolean.FALSE },
75              { "Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE },
76              { "Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE },
77              { "Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE },
78              { "Dave", "Moore", "Green", new Integer(88), Boolean.FALSE },
79              { "Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE },
80              { "Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE },
81              { "Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE },
82              { "Chester", "Rose", "Black", new Integer(0), Boolean.FALSE },
83              { "Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE },
84              { "Georges", "Saab", "Red", new Integer(4), Boolean.FALSE },
85              { "Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE },
86              { "Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE },
87              { "Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE }
88          };
89  
90          // Create a model of the data.
91          @SuppressWarnings("serial")
92          TableModel dataModel = new AbstractTableModel() {
93              // These methods always need to be implemented.
94  
95              public int getColumnCount() {
96                  return names.length;
97              }
98  
99              public int getRowCount() {
100                 return data.length;
101             }
102 
103             public Object getValueAt(int row, int col) {
104                 return data[row][col];
105             }
106 
107             // The default implementations of these methods in
108             // AbstractTableModel would work, but we can refine them.
109             @Override
110             public String getColumnName(int column) {
111                 return names[column];
112             }
113 
114             @Override
115             public Class getColumnClass(int col) {
116                 return getValueAt(0, col).getClass();
117             }
118 
119             @Override
120             public boolean isCellEditable(int row, int col) {
121                 return (col == 4);
122             }
123 
124             @Override
125             public void setValueAt(Object aValue, int row, int column) {
126                 data[row][column] = aValue;
127             }
128         };
129 
130         // Instead of making the table display the data as it would normally
131         // with:
132         // JTable tableView = new JTable(dataModel);
133         // Add a sorter, by using the following three lines instead of the one
134         // above.
135         TableSorter sorter = new TableSorter(dataModel);
136         JTable tableView = new JTable(sorter);
137         sorter.addMouseListenerToHeaderInTable(tableView);
138 
139         JScrollPane scrollpane = new JScrollPane(tableView);
140 
141         scrollpane.setPreferredSize(new Dimension(700, 300));
142         frame.getContentPane().add(scrollpane);
143         frame.pack();
144         frame.setVisible(true);
145     }
146 
147     public static void main(String[] args) {
148         // Trying to set Nimbus look and feel
149         try {
150             for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
151                 if ("Nimbus".equals(info.getName())) {
152                     UIManager.setLookAndFeel(info.getClassName());
153                     break;
154                 }
155             }
156         } catch (Exception ex) {
157             Logger.getLogger(TableExample3.class.getName()).log(Level.SEVERE,
158                     "Failed to apply Nimbus look and feel", ex);
159         }
160         new TableExample3();
161     }
162 }